package myJs.tool

import myJs.cps.Display
import myJs.myPkg.jquery.$
import org.scalajs.dom.window

import scala.scalajs.js.Date
import myJs.Implicits._
import myJs.assets.Running2Gif
import myJs.myPkg.LayerOptions
import org.scalajs.dom.window.localStorage
import scalatags.Text.all._

import scala.scalajs.js

/**
 * Created by yz on 24/4/2022
 */
object Tool {

  val pattern = "yyyy-mm-dd"

  val zhInfo = "信息"
  val layerOptions = LayerOptions(title = zhInfo,closeBtn=0,skin="layui-layer-molv",btn=List())


  val zhRunning = "正在运行"
  val myElement = div(id := "content")(
    span(id := "info")(zhRunning),
    " ",
    img(src := Running2Gif.asInstanceOf[String], width := 30, height := 20, cls := "runningImage")
  ).render

  val confirmButtonClass = "btn-danger"
  val cancelButtonClass = "green"

  def timeBefore = (dateStr: String, dbDateStr: String) => {
    val date = new Date(dateStr)
    val dbDate = new Date(dbDateStr)
    dbDate.setUTCHours(0, 0, 0, 0)
    date.getTime() <= dbDate.getTime()
  }

  def timeAfter = (dateStr: String, dbDateStr: String) => {
    val date = new Date(dateStr)
    val dbDate = new Date(dbDateStr)
    dbDate.setUTCHours(0, 0, 0, 0)
    date.getTime() >= dbDate.getTime()
  }

  def b2Display(b: Boolean) = {
    if (b) Display.block else Display.none
  }

  def selectLeft: Unit = {
    val idInfo = window.location.href.replaceAll(".*/#/", "").
      replaceAll("\\?.*$", "")
    selectLeft(idInfo)
  }

  def selectLeft(idInfo: String): Unit = {
    val tmpIdInfos = idInfo.split("/").toList
    val idInfos = if (tmpIdInfos.head == "member") {
      tmpIdInfos.drop(1)
    } else tmpIdInfos
    val mainId = idInfos.head
    val liId = idInfos.getOrElse(1, "")
    val menuContainer = $(".page-sidebar ul")
    menuContainer.children("li.active").removeClass("active")
    menuContainer.children("li").children("ul").hide()
    $(s"#${mainId} ul>li").removeClass("open")
    $(s"#${mainId}").addClass("active")
    if (liId.nonEmpty) {
      $(s"#${mainId} ul").show()
      $(s"#${mainId} #${liId}").addClass("open")
    }
  }

  def getAccount={
    localStorage.getItem("account")
  }

}
